package com.coffee.takeoutserver.service.impl;

import com.coffee.takeoutserver.bean.*;
import com.coffee.takeoutserver.dao.GoodsInfoMapper;
import com.coffee.takeoutserver.dao.GoodsTypeInfoMapper;
import com.coffee.takeoutserver.dao.SellerMapper;
import com.coffee.takeoutserver.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
class SellerServiceImpl implements SellerService {
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private GoodsTypeInfoMapper goodsTypeInfoMapper;
    @Autowired
    private SellerMapper sellerMapper;

    @Override
    public List<GoodsInfo> getGoodsInfoByTypeId(int typeId) {
        GoodsInfoExample goodsInfoExample = new GoodsInfoExample();
        GoodsInfoExample.Criteria criteria = goodsInfoExample.createCriteria();
        criteria.andTypeidEqualTo(typeId);
        return goodsInfoMapper.selectByExample(goodsInfoExample);
    }

    @Override
    public List<GoodsTypeInfo> getGoodsTypeInfoBySellerId(int sellerId) {
        GoodsTypeInfoExample goodsTypeInfoExample = new GoodsTypeInfoExample();
        GoodsTypeInfoExample.Criteria criteria = goodsTypeInfoExample.createCriteria();
        criteria.andSelleridEqualTo(sellerId);
        return goodsTypeInfoMapper.selectByExample(goodsTypeInfoExample);
    }

    @Override
    public List<GoodsInfo> getGoodsInfoBySellerId(int sellerId) {
        GoodsInfoExample goodsInfoExample = new GoodsInfoExample();
        GoodsInfoExample.Criteria criteria = goodsInfoExample.createCriteria();
        criteria.andSelleridEqualTo(sellerId);
        return goodsInfoMapper.selectByExample(goodsInfoExample);
    }

    @Override
    public List<Seller> getSellerListBySellerIdList(List<Integer> list) {
        if(list==null||list.size()==0){
            return new ArrayList<Seller>();
        }
        SellerExample sellerExample = new SellerExample();
        SellerExample.Criteria criteria = sellerExample.createCriteria();
        criteria.andIdIn(list);
        return sellerMapper.selectByExample(sellerExample);
    }

    @Override
    public Seller getSellerById(int id) {
        SellerExample sellerExample = new SellerExample();
        SellerExample.Criteria criteria = sellerExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Seller> sellerList = sellerMapper.selectByExample(sellerExample);
        return sellerList == null || sellerList.size() == 0 ? null : sellerList.get(0);
    }

    @Override
    public void insertSeller(Seller seller) {
        sellerMapper.insert(seller);
    }

    @Override
    public void updateSellerById(Seller seller) {
        sellerMapper.updateByPrimaryKeySelective(seller);
    }
}
